require 'rubygems'
require 'RMagick'
include Magick

class Page < ActiveRecord::Base
  belongs_to :article
  has_many :tblocks, :dependent => :destroy, :include => :nf_rect
  has_many :words, :dependent => :destroy
  # paperclip examples:
  #   http://github.com/thoughtbot/paperclip/tree/master
  has_attached_file :page_image,
  :styles => { :thumb  => "134x179", :medium => "748" }

  before_destroy :delete_attachments

  def self.show_fxml_params
    { :include => { :tblocks => Tblock.default_fxml_params},
      :methods => [:page_image_width, :page_image_height, :page_image_url, :thumb_page_image_url]}
  end

  def self.index_fxml_params
    { :methods => [:page_image_width, :page_image_height, :page_image_url, :thumb_page_image_url]}
  end

  def page_image_width
    img = ImageList.new(page_image.path)
    img.columns
  end

  def page_image_height
    img = ImageList.new(page_image.path)
    img.rows
  end

  def page_image_url
    page_image.url(:medium)
  end

  def thumb_page_image_url
    page_image.url(:thumb)
  end

  def delete_attachments
    self.page_image.destroy
  end
end
